package com.lrkj.payfee.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.lrkj.common.constant.FeeConstants;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.NumberArithmeticUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.framework.utils.NumberUtils;
import com.lrkj.framework.utils.SerialNumUtil;
import com.lrkj.payfee.domain.AdvanceReceipt;
import com.lrkj.payfee.domain.RoomNorm;
import com.lrkj.payfee.domain.RuleCost;
import com.lrkj.payfee.domain.vo.AdueRecordVo;
import com.lrkj.payfee.domain.vo.PayFeeVo;
import com.lrkj.payfee.factory.PayFeeFactory;
import com.lrkj.payfee.mapper.RuleCostMapper;
import com.lrkj.payfee.service.IAdvanceReceiptService;
import com.lrkj.payfee.service.IRoomNormService;
import com.lrkj.payfee.service.IRuleCostService;
import com.lrkj.payfee.vo.*;
import com.lrkj.property.domain.Room;
import com.lrkj.property.service.IRoomService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 常规费用单Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-30
 */
@Service
public class RuleCostServiceImpl implements IRuleCostService {
    @Autowired
    private RuleCostMapper ruleCostMapper;

    @Autowired
    private IRoomNormService roomNormService;

    @Autowired
    private PayFeeFactory payFeeFactory;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IAdvanceReceiptService advanceReceiptService;


    /**
     * 查询常规费用单
     *
     * @param costId 常规费用单ID
     * @return 常规费用单
     */
    @Override
    public RuleCost selectRuleCostById(String costId) {
        return ruleCostMapper.selectRuleCostById(costId);
    }

    /**
     * 查询常规费用单列表
     *
     * @param ruleCost 常规费用单
     * @return 常规费用单
     */
    @Override
    public List<RuleCost> selectRuleCostList(RuleCost ruleCost) {
        return ruleCostMapper.selectRuleCostList(ruleCost);
    }

    /**
     * 新增常规费用单
     *
     * @param ruleCost 常规费用单
     * @return 结果
     */
    @Override
    public int insertRuleCost(RuleCost ruleCost) {

        return ruleCostMapper.insertRuleCost(ruleCost);
    }

    @Override
    public int edit(final List<RuleCost> ruleCost) {


        return 1;
    }

    /**
     * 修改常规费用单
     *
     * @param ruleCost 常规费用单
     * @return 结果
     */
    @Override
    public int updateRuleCost(RuleCost ruleCost) {
        this.setUnitInfo(ruleCost);
        ruleCost.setUpdateBy(SecurityUtils.getUsername());
        ruleCost.setUpdateTime(DateUtils.getNowDate());
        return ruleCostMapper.updateRuleCost(ruleCost);
    }

    private void setUnitInfo(RuleCost bean) {
        Room parentBean = roomService.selectRoomById(bean.getRoomId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getRoomId());
        bean.setDeptId(parentBean.getDeptId());
    }

    /**
     * 批量删除常规费用单
     *
     * @param costIds 需要删除的常规费用单ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteRuleCostByIds(String[] costIds) {

        return 1;
    }

    /**
     * 删除常规费用单信息
     *
     * @param costId 常规费用单ID
     * @return 结果
     */
    @Override
    public int deleteRuleCostById(String costId) {
        return ruleCostMapper.deleteRuleCostById(costId);
    }


    /**
     * 创建常规费用单信息
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int createRuleCost(RuleCost ruleCost) {
        return 1;
    }

    /**
     * 创建收费单
     *
     * @param normChargeVo 房屋收费标准
     **/
    private RuleCost createRuleCostFactory(NormChargeVo normChargeVo) {
        RuleCost bean = new RuleCost();
        return bean;
    }

    /**
     * 创建房屋收费标准更新对象
     *
     * @param normChargeVo 房屋收费标准
     */
    public RoomNorm createUpdateRoomNorm(NormChargeVo normChargeVo) {
        RoomNorm roomNorm = new RoomNorm();
        PayFeeVo payFeeVo = payFeeFactory.getPayFeeVo(normChargeVo);
        roomNorm.setNextBeginChargeTime(payFeeVo.getBeginTime());
        roomNorm.setNextFinishChargetime(payFeeVo.getFinishTime());
        roomNorm.setReceivableTime(payFeeVo.getReceivableTime());
        roomNorm.setStatus(payFeeVo.getStatus());
        roomNorm.setRoomNormId(normChargeVo.getRoomNormId());
        return roomNorm;
    }

    @Override
    public String getNo(String key) {
        int nextVal = SerialNumUtil.getInstance().nextInt(key);
        return key + NumberUtils.frontCompWithZore(nextVal, 7);
    }

    @Override
    public int deleteRuleCostByCopyId(String[] copyIds) {
        return ruleCostMapper.deleteRuleCostByCopyId(copyIds);
    }

    /**
     * 保证金退款
     **/
    @Override
    public int bailRefund(AdueRecordVo adueRecordVo) {
        return 0;
    }

    //获取房屋收费标准
    private List<RoomNorm> getRoomNormSelect(RuleCost ruleCost) {
       
        return roomNormService.findByList(eg);
    }

    @Override
    public int doSaveList(List<RuleCost> batchBean) {
        List<RuleCost> saveBeans = new ArrayList<>();
        if (batchBean.size() > 500) {
            for (int i = 0; i < batchBean.size(); i++) {
                if ((i + 1) % 500 == 0) {
                    saveBeans.add(batchBean.get(i));
                    ruleCostMapper.doSaveList(saveBeans);
                    saveBeans.clear();
                } else {
                    saveBeans.add(batchBean.get(i));
                }
            }
            if (null != saveBeans && saveBeans.size() > 0) {
                ruleCostMapper.doSaveList(saveBeans);
            }
        } else {
            if (batchBean.size() > 0) {
                ruleCostMapper.doSaveList(batchBean);
            }
        }
        return 1;
    }

    /**
     * 查询房屋费用
     **/
    @Override
    public List<AdueRecordVo> selectAdueRecord(AdueRecordVo adueRecordVo) {

        List<AdueRecordVo> resultList = new ArrayList<>();

        return resultList;
    }

    private AdueRecordVo advanceReceipt(String roomId) {
        AdueRecordVo bean = null;

        return bean;

    }

    private Map<String, RoomNorm> assembleRoomNorm(String roomIds) {
        Map<String, RoomNorm> map = new HashMap<>();

        return map;
    }


    /**
     * 更新结账期间
     **/
    public int updateFiscalPeriod(Map<String, Object> params) {

        return ruleCostMapper.updateFiscalPeriod(params);
    }


    /***
     *
     * 查询账期内生成的账单
     *
     * */
    @Override
    public List<RuleCost> selectFeeFormByFiscalPeriodList(NormChargeVo normChargeVo) {
        return ruleCostMapper.selectFeeFormByFiscalPeriodList(normChargeVo);
    }

    @Override
    public int changeFee(ChangeFeeVo changeFeeVo) {
        if (null != changeFeeVo.getChangeFeeVos() && changeFeeVo.getChangeFeeVos().size() > 0) {
            ruleCostMapper.changeFee(changeFeeVo.getChangeFeeVos());
        }
        return 1;
    }

    @Override
    @Transactional
    public int demolitionFee(DemolitionVo demolitionVo) {
 
        return 1;
    }

    @Override
    public BigDecimal roomAmountOwed(String roomId) {
        return ruleCostMapper.roomAmountOwed(roomId);
    }

    @Override
    public JSONObject roomBillDetail(String roomId) {
        JSONObject result = new JSONObject();

        return result;
    }

    @Override
    public List<RuleCost> selectRuleCostIds(List<String> costIds) {
        return ruleCostMapper.selectRuleCostIds(costIds.toArray(new String[costIds.size()]));
    }

    @Override
    public int updateAmountOwed(List<RuleCost> ruleCosts) {
        return ruleCostMapper.updateAmountOwed(ruleCosts);
    }


    @Override
    public int arrearageCountByRoomId(String roomId) {
        return ruleCostMapper.arrearageCountByRoomId(roomId);
    }
}
